#-*-coding:utf-8-*-
#author:fanerlei
#date:2021.8.17
#https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
# 链表反转
# https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
# 反向打印链表
import math
import random

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x,nextNode):
        self.val = x
        self.next = nextNode



def reverseList(head):
	"""
	:type head: ListNode
	:rtype: ListNode
	"""

	if not head:
		return None

	if not head.next:
		return head
	else:
		preNode = None
		nextNode = head.next
		while (nextNode is not None):

			head.next = preNode
			preNode = head
			head = nextNode
			nextNode = nextNode.next
		# 这一行很重要,因为当next 是None是head刚刚到最后一个Node,但是并没有完成
		# next的改动，所以没有这一行的话就会导致返回的只有一个Node
		head.next = preNode
		return head


def reversePrint(head):
	"""
	:type head: ListNode
	:rtype: List[int]
	"""
	tmpList = []
	if not head:
		return tmpList
	nextNode = head.next
	if not nextNode:
		return [head.val]
	else:
		tmpList.append(head.val)
		while (nextNode is not None):
			head = nextNode
			nextNode = nextNode.next
			tmpList.append(head.val)
		return tmpList[::-1]


if __name__ == "__main__":
	linkNode1 = ListNode(1,None)
	linkNode2 = ListNode(2,linkNode1)
	linkNode3 = ListNode(3, linkNode2)
	linkNode4 = ListNode(4, linkNode3)
	sol = reverseList(linkNode4)
	# print sol.next
	# while(sol):
	# 	print sol.val
	# 	sol = sol.next
	# print sol.val
	tmpNum = 55
	print tmpNum // 2 == math.floor(tmpNum/2)

	for i in xrange(100):
		print random.randint(0, 5)